package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.dto.ResultModel;
import com.cn.wanxi.model.UserModel;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class UserService {
    /**
     * 通过用户名和密码进行查询
     *
     * @param userModel
     * @return
     */
    public ResultModel findByUserName(UserModel userModel) {
        UserDao userDao = new UserImpl();
        List<UserModel> list = new ArrayList<>();
        //根据用户名去数据库查询
        UserModel model = userDao.findByUserName(userModel);
        list.add(model);
        //判断是否登录成功，如果登录成功就需要将用户名存入redis缓存中，否则返回错误信息
        if (userModel.getUsername().equals(model.getUsername())) {
            Jedis jedis = new Jedis();
            jedis.set("username", model.getUsername());
            jedis.expire("username", 30);
        }
        ResultModel resultModel = null;
        if (userModel.getUsername().equals(model.getUsername())) {
            resultModel = new ResultModel("200", "用户名正确，" + model.getUsername() + "欢迎登陆", list);
        } else {
            resultModel = ResultModel.ResultError();
        }
        return resultModel;
    }
}
